<template>
   <view>
      <!-- 搜索条 -->
      <view class="search-box">
         <search-bar placeholder="搜一搜"></search-bar>
      </view>
      <view class="scroll-view-container">
         <!-- 左边的滚动条组件 -->
         <scroll-view :style="{height:h+'px'}" class="left-scroll-view" scroll-y>
            <view v-for="(item,index) in categoryList" :key="item.cat_id"
               :class="['left-scroll-view-item',index===activeIndex?'active':'']" @click="activeIndex=index">
               {{item.cat_name}}
            </view>
         </scroll-view>
         <!-- 右边的滚动条 -->
         <scroll-view :style="{height:h+'px'}" :scroll-top="scrollTop" class="right-scroll-view" scroll-y>
            <!-- 二级标题 -->
            <view v-for="item in level2List" :key="item.cat_id">
               <view class="cate-lv2-title">
                  / {{item.cat_name}} /
               </view>
               <!-- 三级列表 -->
               <view class="cate-lv3-list">
                  <view v-for="subitem in item.children" :key="subitem.cat_id" class="cate-lv3-item" @click="goToGoodsList(subitem.cat_id)">
                     <image :src="subitem.cat_icon" />
                     <text>{{subitem.cat_name}}</text>
                  </view>
               </view>
            </view>
         </scroll-view>
      </view>
   </view>
</template>

<script>
   export default {
      data() {
         return {
            categoryList: [],
            activeIndex: 0,
            h: 0,
            scrollTop: 0
         }
      },
      onLoad() {
         this.getCategoryList()
         // 获取可操作屏幕的高度
         const {
            windowHeight
         } = uni.getSystemInfoSync()
         this.h = windowHeight - 50
      },
      methods: {
         async getCategoryList() {
            const res = await uni.$request({
               url: 'categories'
            })
            if (res.meta.status === 200) {
               this.categoryList = res.message
            }
         },
         goToGoodsList(cid){
            uni.navigateTo({ url: '/subpkg/goods-list/index?cid='+cid })
         }
      },
      computed: {
         level2List() {
            if (this.categoryList.length > 0) {
               return this.categoryList[this.activeIndex].children
            } else {
               return []
            }

         }
      }
   }
</script>

<style lang="scss" scoped>
   .search-box {
      // 设置定位效果为“吸顶”
      position: sticky;
      // 吸顶的“位置”
      top: 0;
      // 提高层级，防止被轮播图覆盖
      z-index: 999;
   }

   .scroll-view-container {
      display: flex;


      .left-scroll-view {
         width: 240rpx;


         .left-scroll-view-item {
            line-height: 60px;
            background-color: #f7f7f7;
            text-align: center;
            font-size: 12px;


            // 激活项的样式
            &.active {
               background-color: #ffffff;
               position: relative;


               // 渲染激活项左侧的红色指示边线
               &::before {
                  content: ' ';
                  display: block;
                  width: 3px;
                  height: 30px;
                  background-color: #c00000;
                  position: absolute;
                  left: 0;
                  top: 50%;
                  transform: translateY(-50%);
               }
            }
         }
      }


      .right-scroll-view {
         .cate-lv2-title {
            font-size: 12px;
            font-weight: bold;
            text-align: center;
            padding: 15px 0;
         }

         .cate-lv3-list {
            display: flex;
            flex-wrap: wrap;


            .cate-lv3-item {
               width: 33.33%;
               margin-bottom: 10px;
               display: flex;
               flex-direction: column;
               align-items: center;


               image {
                  width: 60px;
                  height: 60px;
               }


               text {
                  font-size: 12px;
               }
            }
         }
      }
   }
</style>